// pages/login/login.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    phone:"",
    age:-1,
    genderOptions: ['男', '女'],
    selectedGender: '请选择性别',
    ageOptions: Array.from({ length: 120 }, (value, index) => index + 1).map(age => age + "岁"),
    videoUrl: '',
    selectedAge: '请选择年龄',
    Name:"",
    nickName: "请输入你的昵称",
    defaultImg:"/pages/source/用户.png",
    userInfo:{
      },
    userInfo1:{
  
    },
      code:"",
      hasUserInfo:false,
      canIUseGetUserProfile: false,
    },
    

  /**
   * 组件的方法列表
   */
  methods:{
    submitForm:function(e) { 
      const that = this;
      var Name=e.detail.value.Name;
      wx.setStorageSync('userInfo1', userInfo1);
      console.log(e)
      var q=q=/^[1][3789][\d]{9}$/;
      const age = parseInt(e.detail.value.Age);
      var phone=e.detail.value.phone
      let gender = '';
       if (e.detail.value.Gender === '0') {
                 gender = '男';
          } else if (e.detail.value.Gender === '1') {
                  gender = '女';
                   }
          var userInfo1={
            "name":Name,
            "age": age+1,
            "phone": phone,
            "gender": gender,
                      }
  
          if(phone!== ""){
            if(!q.test(phone)){
              wx.showModal({
                title: "提示",
                content: "手机号格式不正确",
                showCancel: false
              });
                }else{
                  wx.request({
                    url: 'http://localhost:8080/jscode2session',
                    method:'post',
                    data:{
                     code: that.data.code,
                     userInfo1:userInfo1
                    },
                    success: res => {
                      console.log(res.data);
                      wx.showToast({
                        title:'提交成功',
                        icon:'success',
                        duration:1000,
                        success: function (e) {
                        setTimeout(function () {
                          wx.switchTab({
                        url: '/pages/me/me',
                        success: () => {
                          that.setData({
                            userInfo1:wx.getStorageSync('userInfo1'),
                            hasUSerInfo:true
                          })
                        }
                      })
                          }, 1000);}
                      })
                      },
                      fail: err => {
                       console.error(err);
                       wx.showToast({
                         title:'保存失败',
                         icon:'loading',
                         duration:1000
                       })
                     }
                        });
               }
             }else{
               console.log(phone),
               console.log(age),
               console.log(gender),
              console.log(userInfo1),
              wx.request({
                url: 'http://localhost:8080/jscode2session',
                method:'post',
                data:{
                 code: that.data.code,
                 userInfo1:userInfo1
                },
                success: res => {
                  console.log(res.data);
                  },
                  fail: err => {
                   console.error(err);
                   wx.showToast({
                     title:'保存失败',
                     icon:'loading',
                     duration:1000
                   })
                 }
                    });
              wx.showToast({
                     title:'提交成功',
                     icon:'success',
                     duration:1000,
                     success: function (e) {
                     setTimeout(function () {
                       wx.switchTab({
                     url: '/pages/me/me',
                     success: () => {
                       that.setData({
                         userInfo1:wx.getStorageSync('userInfo1'),
                         hasUSerInfo:true
                       })
                     }
                   })
                       }, 1000);}
                   })
             }
            } ,

  post:function(e){
    const that=this
    wx.login({
      success: function(res) {
        console.log(res);
        const code=res.code;
        that.setData({
          code
        })
      },
      fail: function(res) {
        console.log('绑定失败');
      },
      
    })

  },

    getUserProfile:function(){
      const that = this;
      
      wx.login({
        success: function(res) {
          console.log(res);
          const code=res.code;
          that.setData({
            code
          })
        },
        fail: function(res) {
          console.log('绑定失败');
        },
        
      })
     
      wx.getUserProfile({
        desc: '获取用户信息',
        success: (res) => {
          const userInfo = res.userInfo;
          var reg_phone = /^[1][3789][\d]{9}$/;
          var phone=this.data.phone
          console.log(this.data.code)
          console.log(userInfo)
          wx.setStorageSync('userInfo', userInfo);
          if (this.data.phone === "" || this.data.Name === "" || this.data.selectedGender === "请选择性别" ||this.data.selectedAge === "请选择年龄") {
            wx.showModal({
              title: '确认操作',
              content: '您的信息还未完善，确定保存嘛',
              success: function (res) {
                if (res.confirm) {
                 
                  if(phone!== ""){
                    if(!reg_phone.test(phone)){
                      wx.showModal({
                        title: "提示",
                        content: "手机号格式不正确",
                        showCancel: false
                      });
                    }else{
                      wx.request({
                        url: 'http://localhost:8080/jscode2session',
                        method: 'POST',
                        data: {
                          code: that.data.code,
                          userInfo: userInfo
                        },
                        success: res => {
                          // 处理返回结果
                          console.log(res.data);
                          // 在这里可以根据需要进行后续操作
                        },
                        fail: err => {
                          console.error(err);
                        }
                      });
                      wx.showToast({
                        title:'提交成功',
                        icon:'success',
                        duration:1000,
                        success: function (e) {
                        setTimeout(function () {
                          wx.switchTab({
                        url: '/pages/me/me',
                        success: () => {
                          that.setData({
                            userInfo:wx.getStorageSync('userInfo'),
                            hasUSerInfo:true
                          })
                        }
                      })
                          }, 1000);}
                      })
                    }
                  }else{
                    wx.request({
                      url: 'http://localhost:8080/jscode2session',
                      method: 'POST',
                      data: {
                        code: that.data.code,
                        userInfo: userInfo
                      },
                      success: res => {
                        // 处理返回结果
                        console.log(res.data);
                        // 在这里可以根据需要进行后续操作
                      },
                      fail: err => {
                        console.error(err);
                      }
                    });
                    wx.showToast({
                      title:'绑定成功',
                      icon:'success',
                      duration:1000,
                      success: function (e) {
                      setTimeout(function () {
                        wx.switchTab({
                      url: '/pages/me/me',
                      success: () => {
                        that.setData({
                          userInfo:wx.getStorageSync('userInfo'),
                          hasUSerInfo:true
                        })
                      }
                    })
                        }, 1000);}
                    })
                  }

                     
                 
                  // 用户点击了确认按钮
                  // 进行操作
                } else if (res.cancel) {
                  // 用户点击了取消按钮
                  // 取消操作
                }
              }
            })
           ;
          }else{
            wx.request({
              url: 'http://localhost:8080/jscode2session',
              method: 'POST',
              data: {
                code: that.data.code,
                userInfo: userInfo
              },
              success: res => {
                // 处理返回结果
                console.log(res.data);
                // 在这里可以根据需要进行后续操作
                wx.showToast({
                  title:'绑定成功',
                  icon:'success',
                  duration:500,
                  success: function (e) {
                  setTimeout(function () {
                    wx.switchTab({
                  url: '/pages/me/me',
                  success: () => {
                    that.setData({
                      userInfo:wx.getStorageSync('userInfo'),
                      hasUSerInfo:true
                    })
                  }
                })
                    }, 500);}
                })
              },
              fail: err => {
                console.error(err);
              }
            });
            
          }

         
         
         
        }
      })
    },
    onGenderChange: function (e) {
      const selectedIndex = e.detail.value;
      const selectedGender = this.data.genderOptions[selectedIndex];
      console.log(selectedIndex)
      console.log(selectedGender)
      this.setData({
        selectedGender: selectedGender
      });
    },
    onAgeChange: function (e) {
      const selectedIndex = e.detail.value;
      const selectedAge = this.data.ageOptions[selectedIndex];
      console.log(e.detail.value)
      this.setData({
        
        selectedAge: selectedAge
      });
    },
    getName: function(e) {
      console.log(e.detail.value); 
      this.setData({
        Name: e.detail.value // 将输入的值赋给 Name 变量
      }); // 将输入的内容打印到控制台
    },
    goto: function(e) {
      const that = this;
      wx.switchTab({
        url: '/pages/me/me',
        success: () => {
          that.setData({
            userInfo:wx.getStorageSync('userInfo'),
            hasUSerInfo:true
          })
        }
      })
    },
    getphone: function(e) {
      console.log(e.detail.value); 
      this.setData({
        phone: e.detail.value
      }); // 将输入的内容打印到控制台
    },
    getnickName: function(e) {
      console.log(e.detail.value);
      this.setData({
        nickName: e.detail.value // 将输入的值赋给 Name 变量
      });
    },
    

    onLoad: function(options) {
      
      this.post()
      this.fetchVideoUrl();
      const userInfo = wx.getStorageSync('userInfo');
      const userInfo1=wx.getStorageSync('userInfo1');
      const nickName=userInfo.nickName
      if (userInfo){
        this.setData({
          hasUserInfo:true,
          userInfo: userInfo,
          nickName:nickName
        })
      } 
      if (userInfo1){
        this.setData({
          
          userInfo1: userInfo1,
         
        })
      }

      
    },
    fetchVideoUrl: function () {
      wx.request({
        url: 'http://localhost:8080/getVideo',
        success: (res) => {
          const dynamicVideoUrl = res.data; // 假设后端接口返回视频URL的字段为"url"
          this.setData({
            videoUrl: dynamicVideoUrl
          });
        },
        fail: (error) => {
          console.log('请求视频URL失败', error);
        }
      });
    },
  
    videoErrorCallback: function (e) {
      console.log('视频加载错误:', e.detail.errMsg);
    }
    
}})
